package com.globalwave.pos.user.service;


import static junit.framework.Assert.*;

import org.junit.Test;

import com.globalwave.common.exception.BusinessException;
import com.globalwave.system.entity.ShopProfile;
import com.globalwave.system.entity.TerminalProfile;
import com.globalwave.system.entity.User;


public class PosLoginTest extends PosBaseTestCase {

	/*
	 * RT01   R1     R2
	 * O      C      C
	 * R1-Open
	 * O      O      C
	 * R2-Open
	 * O      O      O
	 * R2-Close
	 * O      O      C
	 * RT01-Close
	 * C      C      C
	 * R1-Open
	 * Exception
	 * RT01-Open
	 * O      C      C
	 * 
	 */
	@Test
	public void login() throws Exception {
		execute(getSqlFileDir() + "TerminalProfileServiceTest_dayBegin_OK.sql") ;
		TerminalProfile t$r1 = getTerminalProfileR1() ;
		TerminalProfile t$r2 = getTerminalProfileR2() ;
		ShopProfile c$1 = getShopRT01();

		assertStatus("O", "C", "C") ;
	    getTerminalProfileService().dayBeginAndLogin(t$r1, getUserRT010078()) ;
	    assertStatus("O", "O", "C") ;

	    getTerminalProfileService().dayBeginAndLogin(t$r2, getUserRT010079()) ;
	    assertStatus("O", "O", "O") ;

	    getTerminalProfileService().dayEnd(t$r2) ;
	    assertStatus("O", "O", "C") ;

	    getShopProfileService().dayEnd(c$1) ;
	    assertStatus("C", "C", "C") ;
	    

		try {

		    getTerminalProfileService().dayBeginAndLogin(t$r1, getUserRT010078()) ;

			assertFalse("must be throw exception!", true) ;
			
		} catch (BusinessException be) {
			assertEquals("sale day too early!", "E0001020010", be.getMessageKey()) ;
		}		
		
		getShopProfileService().dayBegin(c$1, getUserRT010078()) ;

		assertStatus("O", "C", "C") ;
	}
	
	private void assertStatus(String s1status, String t1status, String t2status) throws Exception {
		
		ShopProfile s = getShopProfileService().get(regionCode, shopCode) ;
		assertEquals(s1status, s.getStatus_()) ;
		
		TerminalProfile t = getTerminalProfileService().get(regionCode, terminalCode) ;
		assertEquals(t1status, t.getStatus_()) ;
		
		t = getTerminalProfileService().get(regionCode, terminalCodeR2) ;
		assertEquals(t2status, t.getStatus_()) ;
	}

	private TerminalProfile getTerminalProfileR1() throws Exception {
		TerminalProfile e = new TerminalProfile() ;
		e.setRegion_code(regionCode) ;
		e.setCode_(terminalCode) ;
		e.setSale_date(getDate("20101001")) ;
		e.setComputer_ip("192.168.0.99") ;
		e.setComputer_name("Test XP") ;
		return e ;
	}

	private TerminalProfile getTerminalProfileR2() throws Exception {
		TerminalProfile e = new TerminalProfile() ;
		e.setRegion_code(regionCode) ;
		e.setCode_(terminalCodeR2) ;
		e.setSale_date(getDate("20101001")) ;
		e.setComputer_ip("192.168.0.98") ;
		e.setComputer_name("Test XP-2") ;
		return e ;
	}

	
	private User getUserRT010078() {
		User e = new User() ;
		e.setRegion_code(regionCode) ;
		e.setLogin_id(userId) ;
		e.setShop_code(shopCode);

		return e ;
		
	} 
	
	private User getUserRT010079() {
		User e = new User() ;
		e.setRegion_code(regionCode) ;
		e.setLogin_id("RT010079") ;
		e.setShop_code(shopCode);

		return e ;
		
	} 
	

	private ShopProfile getShopRT01() throws Exception {
		ShopProfile e = new ShopProfile() ;
		e.setRegion_code(regionCode) ;
		e.setCode_(shopCode) ;
		e.setSale_date(getDate("20101001")) ;
		return e ;
	}
}
